package com.beordie.dao.impl;

import com.beordie.dao.IRoleDao;
import com.beordie.model.Menu;
import com.beordie.model.Role;
import com.beordie.model.Users;
import com.beordie.utils.DBUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname RoleDaoImpl
 * @Description TODO
 * @Date 2021/6/13 22:20
 * @Created 30500
 */
public class RoleDaoImpl extends DBUtils implements IRoleDao {
    @Override
    public int getAllTotal() {
        // 查询语句
        String sql = "select count(*) from role";

        // 查询数据库信息
        resultSet = super.query(sql, null);
        int result = 0;
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充信息
                    result = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                super.close();
            }
        }
        return result;
    }

    @Override
    public List<Role> getByLimit(int index, int pageSize) {
        // 查询语句
        String sql = "select * from role limit ?,?";
        List<Integer> params = new ArrayList<>();
        params.add((index - 1) * pageSize);
        params.add(pageSize);

        // 查询数据库信息
        resultSet = super.query(sql, params);
        List<Role> roles = new ArrayList<>();
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充角色信息
                    Role role = new Role();
                    role.setRoleName(resultSet.getString("rolename"));
                    role.setRoleId(resultSet.getInt("roleid"));
                    role.setRoleState(resultSet.getInt("rolestate"));
                    roles.add(role);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                super.close();
            }
        }
        return roles;
    }

    @Override
    public List<Role> getAll() {
        // 查询语句
        String sql = "select * from role";

        // 查询数据库信息
        resultSet = super.query(sql, null);
        List<Role> roles = new ArrayList<>();
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充角色信息
                    Role role = new Role();
                    role.setRoleName(resultSet.getString("rolename"));
                    role.setRoleId(resultSet.getInt("roleid"));
                    role.setRoleState(resultSet.getInt("rolestate"));
                    roles.add(role);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                super.close();
            }
        }
        return roles;
    }

    @Override
    public int add(Role role) {
        // 查询语句
        String sql = "insert into role values(null,?,?)";

        List<Object> params = new ArrayList<>();
        params.add(role.getRoleName());
        params.add(role.getRoleState());

        // 插入数据库信息
        super.update(sql, params);
        statement = super.getStatement();
        int result = 0;
        try {
            ResultSet resultSet = statement.getGeneratedKeys();
            while (resultSet.next()) {
                // 获取 ID
                result = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.close();
        }

        return result;
    }

    @Override
    public Role getById(int roleId) {
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();

        String sql = "select rolename,rolestate,m.menuid,menuname,upmenuid,state,url " +
                "from role r,middle mi,menu m where r.roleid=mi.roleid and mi.menuid=m.menuid and r.roleid=?";
        List<Integer> params = new ArrayList<>();

        params.add(roleId);
        resultSet = super.query(sql, params);
        try {
            while (resultSet.next()) {
                Menu menu = new Menu();
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                menu.setUrl(resultSet.getString("url"));
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setMenuName(resultSet.getString("menuname"));
                menuList.add(menu);
            }
            role.setMenu(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.close();
        }
        role.setRoleId(roleId);
        return role;
    }

    @Override
    public int update(Role role) {
        String sql = "update role set rolename=?,rolestate=? where roleid=?";
        List<Object> params = new ArrayList<>();
        params.add(role.getRoleName());
        params.add(role.getRoleState());
        params.add(role.getRoleId());

        int result = 0;
        try {
            result = super.update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.close();
        }
        return result;
    }

    @Override
    public int delete(int roleId) {
        String sql = "delete from role where roleid=?";
        List<Integer> params = new ArrayList<>();

        params.add(roleId);
        int result = 0;
        try {
            result = super.update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.close();
        }
        return result;
    }
}
